﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Module.Club
{
    public partial class ClubHire : System.Web.UI.Page
    {
        private int userId;
        private int cUserId;
        private int clubId;
        private int venuesJType;
        protected string ModuleId;
        private Jy123.Model.Club_Main club;
        private Jy123.Model.Module_Main module;
        private Jy123.Model.Public_Level level;
        private Jy123.Model.User_Main user;
        private string[] strContent = { "我的球队", "球队信息", "比赛实况", "球队道具", "球队排行", "球队训练", "商业活动" };
        private string[] strCss = { "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu2", "lmenu3" };
        private string[] strHeaderCss = { "", "", "NavIcon", "" };


        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            if (!Page.IsPostBack)
            {
                cUserId = int.Parse(strUserIdFromCookie);
                LoadInitialValue();
            }
        }

        private void LoadInitialValue()
        {
            string key = "ClubId";
            string clubIdString = Request.QueryString[key];
            if (!string.IsNullOrEmpty(clubIdString))
            {
                clubId = int.Parse(clubIdString);
                ModuleId = clubIdString;
                Jy123.BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Jy123.BLL.Module.Module BLLModule = new Jy123.BLL.Module.Module();
                Jy123.BLL.Module_Main BLLModuleMain = new Jy123.BLL.Module_Main();
                Jy123.BLL.Venues.VModule BLLVModule = new Jy123.BLL.Venues.VModule();
                Jy123.BLL.Public_Level BLLLevel = new Jy123.BLL.Public_Level();
                Jy123.BLL.User_Main BLLUser = new Jy123.BLL.User_Main();

                club = BLLClub.GetModel(clubId);
                userId = club.FUser.Value;
                user = BLLUser.GetModel(userId);
                level = BLLLevel.GetModel(club.FLevel.Value);
                module = BLLModuleMain.GetModel(clubId);
                BindHTML();
            }
            else
            {
                //找不到UserId，返回首页,目前暂时返回空值
            }
        }

        private void BindHTML()
        {
            string strHTML = Cache["clubhire"].ToString();
            Jy123.Web.AppCode.ModuleClub appModule = new Jy123.Web.AppCode.ModuleClub();
            string strHeader = Cache["header"].ToString();
            string headerString = appModule.BindHeader(strHeader, strHeaderCss);
            string strLeft = Cache["leftmenu"].ToString();
            string strLeftLi = Cache["leftmenuli"].ToString();
            string strTail = "?ClubId=" + club.ID.ToString();

            BLL.Race.Race BLLRace = new Jy123.BLL.Race.Race();
            int cVId = BLLRace.GetCurrentRaceVenues(club.ID);
            string trainString = "javascript:$.moonbox.warning('您的俱乐部并未进行训练')";
            if (club.CStatus == Convert.ToInt32(BLL.Public.EnumConfig.ClubStatus.Training))
            {
                BLL.TCenter_ClubTC BLLTC = new Jy123.BLL.TCenter_ClubTC();
                string strTWhere = string.Format("FClub = {0} AND TrainingTime IS NULL", club.ID.ToString());
                List<Model.TCenter_ClubTC> listTC = BLLTC.GetModelList(strTWhere);
                if (listTC.Count > 0)
                {
                    Model.TCenter_ClubTC TC = listTC[0];
                    trainString = "/Module/TCenter/TrainInfo.aspx?TCenterId=" + TC.FTCenter.Value.ToString();
                }
            }
            string[] strHref = { "MyClub.aspx" + strTail, "ClubInfo.aspx" + strTail, "/Module/Venues/VenuesRace.aspx?VenuesId=" + cVId.ToString(), "ClubProps.aspx" + strTail, "ModuleRank.aspx" + strTail, trainString, "#" };
            string[] strNoRaceHref = { "MyClub.aspx" + strTail, "ClubInfo.aspx" + strTail, "javascript:$.moonbox.warning('您的俱乐部并未参赛')", "ClubProps.aspx" + strTail, "ModuleRank.aspx" + strTail, trainString, "#" };
            string leftMenuString;
            if (cVId > 0)
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strHref, strCss);
            }
            else
            {
                leftMenuString = appModule.BindLeftMenu(strLeft, strLeftLi, strContent, strNoRaceHref, strCss);
            }

            string strClub = Cache["contentclub"].ToString();
            string contentString = appModule.BindContentClub(strClub, module, club, user, level);
            string strMain = Cache["mainmenu"].ToString();
            string strDiv = Cache["mainmenusubdiv"].ToString();
            string strLi = Cache["mainmenusubdivli"].ToString();
            string mainMenuString = appModule.BindMainMenu(strMain, strDiv, strLi, cUserId);

            strHTML = strHTML.Replace("$Header$", headerString);
            strHTML = strHTML.Replace("$MainMenu$", mainMenuString);
            if (cUserId == userId)
            {
                strHTML = strHTML.Replace("$LeftMenu$", leftMenuString);
            }
            else
            {
                strHTML = strHTML.Replace("$LeftMenu$", string.Empty);
            }

            strHTML = strHTML.Replace("$ContentClub$", contentString);
            strHTML = strHTML.Replace("$Footer$", Cache["footer"].ToString());
            strHTML = strHTML.Replace("$ClubName$", club.CName);
            strHTML = strHTML.Replace("$ModuleId$", club.ID.ToString());
            Jy123.BLL.Public_Hire BLLHire = new Jy123.BLL.Public_Hire();
            List<Jy123.Model.Public_Hire> listHire = BLLHire.GetModelList("FJType = " + club.FJType.Value.ToString());
            StringBuilder sbHire = new StringBuilder();
            foreach (Jy123.Model.Public_Hire hire in listHire)
            {
                string strHire = Cache["venueshireli"].ToString();
                strHire = strHire.Replace("$ImageSrc$", hire.Logo);
                strHire = strHire.Replace("$CheckBoxId$", "checkBox" + hire.ID.ToString());
                strHire = strHire.Replace("$Value$", hire.ID.ToString());
                strHire = strHire.Replace("$HireIdHidden$", "hidden" + hire.ID.ToString());
                strHire = strHire.Replace("$HireName$", hire.HName);
                strHire = strHire.Replace("$HirePrice$", (Convert.ToInt32(hire.Price.Value)).ToString() + "/小时");
                sbHire.Append(strHire);
            }
            if (string.IsNullOrEmpty(sbHire.ToString()))
            {
                strHTML = strHTML.Replace("$Li$", "没有租用项目");
            }
            else
            {
                strHTML = strHTML.Replace("$Li$", sbHire.ToString());
            }
            Response.Write(strHTML);
        }
    }
}
